package com.navinfo.opentsp.qingqi.trackoptimize.common.handler;

import com.navinfo.opentsp.qingqi.trackoptimize.common.command.Command;
import com.navinfo.opentsp.qingqi.trackoptimize.common.dto.IntegralCommandList;
import com.navinfo.opentsp.qingqi.trackoptimize.common.result.CommonResult;
import com.navinfo.opentsp.qingqi.trackoptimize.common.validation.ValidateUtil;
import com.navinfo.opentsp.qingqi.trackoptimize.common.validation.group.GroupCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by zhangyu on 2016/11/07.
 */
public abstract class BaseAbstractHandler<C extends Command<?>, CR extends CommonResult> extends
    AbstractCommandHandler<C, CR>
{
    protected static final Logger logger = LoggerFactory.getLogger(BaseAbstractHandler.class);
    
    protected BaseAbstractHandler(Class<C> commandType, Class<CR> resultType)
    {
        super(commandType, resultType);
    }

    @Autowired
    private IntegralCommandList integralFlg;

//    @Autowired
//    private IntegralCommandHandler inHandler;

    public CR handle(C command)
    {
        String validateMsg = ValidateUtil.validateCommand(command, GroupCommand.class);
        if (validateMsg != null)
        {
            CR result = null;
            try
            {
                result = this.getResultType().newInstance();
                // 入参异常提示
                result.setResultCode(507);
                result.setMessage(validateMsg);
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }

            return result;
        }
        //积分配置参数处理
        CR handleResult = businessHandle(command);
//        if("true".equals(integralFlg.getIntegralFlg)) {
//            inHandler.toIntegral(command, handleResult);
//        }
        return handleResult;
    }
    
    protected abstract CR businessHandle(C command);
}
